
require "Object"
require "WorldMap"
--Player class
HUD = Object:new{
	hudFile = "ui/hud.cc2d",
	updateID = nil,
	world = nil,
}

function HUD:onEnter()
	Misc:log("HUD : onEnter .. " .. Misc:getNodeName(self))
	
	--regist the map button event
	local mapBtn = tolua.cast(Misc:getChildByName(self, "mapBtn"), "CCMenuItemImage")
	local function clickMapBtn()
		self:clickMapBtn()
	end
	mapBtn:registerScriptHandler(clickMapBtn)
	
	local function update(dt)
        self:tick(dt)
    end
	self.updateID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, 1/60.0, false)
end

function HUD:onExit()
	CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.updateID)
end

function HUD:create(world)
	local newHUD = HUD:new()
	--self.__index = self
	newHUD.world = world
	
	local cc2dNode = CC2DFileCache:sharedCC2DFileCache():addCC2DNode(self.hudFile)
	ccHUD = tolua.cast(cc2dNode:createCCNode(newHUD.world), "CCLayer")
	Misc:setNodeName(ccPlayer, "hud")
	
	tolua.setpeer(ccHUD, newHUD)
	
	local function onEnterOrExit(eventType)
		if eventType == kCCNodeOnExit then
			ccHUD:onExit()
		else
			ccHUD:onEnter()
		end
	end
	
	ccHUD:registerScriptHandler(onEnterOrExit)
	
	if ccHUD:isRunning() then
		ccHUD:onEnter()
	end
	return ccHUD
end

function HUD:setLife(life)
	local ccLifeProgress = tolua.cast(Misc:getChildByName(self, "life"), "CCProgressTimer")
	ccLifeProgress:setPercentage(life)
end

function HUD:clickMapBtn()
	WorldMap:create("map1/map.cc2d")
end

function HUD:tick(dt)
end